package com.fick.struts.action;
/**
 * 系统菜单
 * add by 王浩强
 */
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.fick.hibernate.dao.SysMenuDAO;
import com.fick.hibernate.model.SysMenu;


public class SysMenuAction extends DispatchAction {

//	private static final Log log = LogFactory.getLog(SysMenuAction.class);
	
	private int id=0;
	private int parid=0;
	private String menuName="";
	private String menuUrl="";
	private int menuOrder=0;
	private int menuShow=1;
	private String menuAbout="";
	private SysMenuDAO dao=new SysMenuDAO();
	
	private void initPara(HttpServletRequest request){
		id=0;
		parid=0;
		menuName="";
		menuUrl="";
		menuOrder=0;
		menuShow=1;
		menuAbout="";
		if (request.getParameter("id")!=null && request.getParameter("id").trim().length()>0) {
			try {
				id=Integer.parseInt(request.getParameter("id"));
			} catch (Exception e) {
				id=0;
			}
		}
		if (request.getParameter("parid")!=null && request.getParameter("parid").trim().length()>0) {
			try {
				parid=Integer.parseInt(request.getParameter("parid"));
			} catch (Exception e) {
				parid=0;
			}
		}
		if (request.getParameter("menuName")!=null && request.getParameter("menuName").trim().length()>0) {
				menuName=request.getParameter("menuName");
		}
		if (request.getParameter("menuUrl")!=null ) {
				menuUrl=request.getParameter("menuUrl");
		}
		if (request.getParameter("menuOrder")!=null ) {
			try {
				menuOrder=Integer.parseInt(request.getParameter("menuOrder"));
			} catch (Exception e) {
				menuOrder=0;
			}
		}
		if (request.getParameter("menuShow")!=null ) {
			try {
				menuShow=Integer.parseInt(request.getParameter("menuShow"));
			} catch (Exception e) {
				menuShow=0;
			}
		}
		if (request.getParameter("menuAbout")!=null ) {
			menuAbout=request.getParameter("menuAbout");
		}
	}
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		initPara(request);
		SysMenu sysMenu=new SysMenu();
		if (parid!=0) {
			sysMenu.setParId(dao.findById(parid));
		}
		sysMenu.setMenuName(menuName);
		sysMenu.setMenuOrder(menuOrder);
		sysMenu.setMenuShow(menuShow);
		sysMenu.setMenuUrl(menuUrl);
		sysMenu.setMenuAbout(menuAbout);
		dao.save(sysMenu);
		return mapping.findForward("menu");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		initPara(request);
		SysMenu sysMenu=dao.findById(id);
		if (sysMenu!=null) {
			sysMenu.setMenuName(menuName);
			sysMenu.setMenuOrder(menuOrder);
			sysMenu.setMenuShow(menuShow);
			sysMenu.setMenuUrl(menuUrl);
			sysMenu.setMenuAbout(menuAbout);
			dao.update(sysMenu);
		}
		return mapping.findForward("menu");
	}
	public ActionForward del(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		if (request.getParameter("id")!=null && request.getParameter("id").trim().length()>0) {
			try {
				id=Integer.parseInt(request.getParameter("id"));
			} catch (Exception e) {
				id=0;
			}
		}
		SysMenu sysMenu=dao.findById(id);
		if (sysMenu!=null) {
			dao.delete(sysMenu);
		}
		return mapping.findForward("menu");
	}
}
